﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Zo.ExcelBox.ViewModels;

namespace Zo.ExcelBox
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewVM(this);

            MainFrame.Navigate(new Uri("Pages/DataTransformPage.xaml", UriKind.Relative));

        }

        private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (sender is System.Windows.Controls.ScrollViewer scrollViewer)
            {
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - e.Delta);
                e.Handled = true;
            }
        }

        private void FolderTreeView_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
        {
            // 取消 TreeView 的自动滚动行为
            e.Handled = true;
        }



        // 点击添加文件夹按钮
        private void AddFolder_Click(object sender, RoutedEventArgs e)
        {
            using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                dialog.Description = "请选择一个文件夹";
                dialog.ShowNewFolderButton = false;

                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string folderPath = dialog.SelectedPath;
                    var folderNode = CreateFolderNode(folderPath);
                    //FolderTreeView.Items.Add(folderNode);

                }
            }
        }

        // 创建文件夹节点（包括文件夹和文件列表）
        private TreeViewItem CreateFolderNode(string folderPath)
        {
            var dirInfo = new DirectoryInfo(folderPath);
            var folderItem = new TreeViewItem
            {
                Header = $"📁 {dirInfo.Name}",
                Tag = folderPath,
                IsExpanded = true
            };

            try
            {
                // 添加该文件夹下的文件
                foreach (var file in dirInfo.GetFiles())
                {
                    var checkBox = new CheckBox
                    {
                        Content = new TextBlock
                        {
                            Text = file.Name,
                            TextTrimming = TextTrimming.CharacterEllipsis,
                            ToolTip = file.FullName,
                            Width = 160, // 控制最大显示宽度
                            VerticalAlignment = VerticalAlignment.Center
                        }
                    };

                    var fileItem = new TreeViewItem
                    {
                        Header = checkBox
                    };

                    folderItem.Items.Add(fileItem);
                }

                // 递归添加子文件夹（如果有）
                //foreach (var subDir in dirInfo.GetDirectories())
                //{
                //    folderItem.Items.Add(CreateFolderNode(subDir.FullName));
                //}
            }
            catch
            {
                // 忽略无法访问的文件夹
            }

            return folderItem;
        }

        private void TreeViewItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (sender is Button btn && btn.Tag is string pageUri)
            {
                MainFrame.Navigate(new Uri(pageUri, UriKind.Relative));
            }
        }

        private void mainFrame_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            outerScrollViewer.ScrollToVerticalOffset(outerScrollViewer.VerticalOffset - e.Delta);
            e.Handled = true;
        }

        private void SideMenuControl_SelectionChanged(object sender, HandyControl.Data.FunctionEventArgs<object> e)
        {
            var item = e.Info as SideMenuItem;

            if (item != null) 
            {
                var url = item.Tag.ToString();
                MainFrame.Navigate(new Uri(url, UriKind.Relative));
            }

        }
    }
}
